﻿package mbalib.ui.controls
{
	import flash.events.*;
	
	import mbalib.ui.constants.BUIConstants;
	import mbalib.ui.core.*;
	import mbalib.ui.events.*;
	
	public class ScrollBar extends BUI
	{
		private var _minScrollPosition:Number = 0;
		private var _pageSize:Number = 10;
		private var _orientation:String = "vertical";
		protected var isDragging:Boolean = false;
		private var _maxScrollPosition:Number = 0;
		private var _lineScrollSize:Number = 1;
		protected var up:Button;
		protected var down:Button;
		private var _scrollPosition:Number = 0;
		private var thumbScrollOffset:Number;
		protected var track:Button;
		protected var thumb:LabelButton;
		private var _pageScrollSize:Number = 0;
		public static var defaultStyles:Object = {skin:"skins.ScrollBar_Skin"};
		public static var WIDTH:int = 11;
		public static const THUMB_MIN_HEIGHT:int = 10;
		
		public function ScrollBar()
		{
			return;
		}// end function
		
		override public function set enabled(param1:Boolean) : void
		{
			var _loc_2:*;
			super.enabled = param1;
			if (enabled)
			{
				_loc_2 = this._maxScrollPosition > this._minScrollPosition;
				this.up.enabled = this._maxScrollPosition > this._minScrollPosition;
				this.thumb.enabled = _loc_2;
				this.track.enabled = _loc_2;
				this.down.enabled = _loc_2;
				this.updateThumb();
			}// end if
			return;
		}// end function
		
		override public function setSize(param1:Number, param2:Number, param3:Boolean = true) : void
		{
			if (this._orientation == BUIConstants.HORIZONTAL)
			{
				super.setSize(param2, param1, param3);
			}
			else
			{
				super.setSize(param1, param2, param3);
			}// end else if
			return;
		}// end function
		
		public function set lineScrollSize(param1:Number) : void
		{
			if (param1 > 0)
			{
				this._lineScrollSize = param1;
			}// end if
			return;
		}// end function
		
		public function get minScrollPosition() : Number
		{
			return this._minScrollPosition;
		}// end function
		
		protected function updateThumb() : void
		{
			var _loc_1:* = this._maxScrollPosition - this._minScrollPosition + this._pageSize;
			if (this.track.height <= THUMB_MIN_HEIGHT || this._maxScrollPosition <= this._minScrollPosition || _loc_1 == 0 || isNaN(_loc_1))
			{
				this.thumb.height = THUMB_MIN_HEIGHT;
				this.thumb.visible = false;
			}
			else
			{
				this.thumb.height = Math.max(THUMB_MIN_HEIGHT + 1, this._pageSize / _loc_1 * this.track.height);
				this.thumb.y = this.track.y + (this.track.height - this.thumb.height) * ((this._scrollPosition - this._minScrollPosition) / (this._maxScrollPosition - this._minScrollPosition));
				this.thumb.visible = enabled;
			}// end else if
			return;
		}// end function
		
		public function set minScrollPosition(param1:Number) : void
		{
			this.setScrollProperties(this._pageSize, param1, this._maxScrollPosition);
			return;
		}// end function
		
		public function setScrollPosition(param1:Number, param2:Boolean = true) : void
		{
			var _loc_3:* = this.scrollPosition;
			this._scrollPosition = Math.max(this._minScrollPosition, Math.min(this._maxScrollPosition, param1));
			if (_loc_3 == this._scrollPosition)
			{
				return;
			}// end if
			if (param2)
			{
				dispatchEvent(new ScrollEvent(this._orientation, this.scrollPosition - _loc_3, this.scrollPosition));
			}// end if
			this.updateThumb();
			return;
		}// end function
		
		public function get maxScrollPosition() : Number
		{
			return this._maxScrollPosition;
		}// end function
		
		public function get scrollPosition() : Number
		{
			return this._scrollPosition;
		}// end function
		
		public function set orientation(param1:String) : void
		{
			if (this._orientation == param1)
			{
				return;
			}// end if
			this._orientation = param1;
			var _loc_2:* = this._orientation == BUIConstants.HORIZONTAL;
			if (_loc_2 && rotation == 0)
			{
				rotation = -90;
				scaleX = -1;
			}
			else if (!_loc_2 && rotation == -90)
			{
				rotation = 0;
				scaleX = 1;
			}// end else if
			invalidate(Invalidation.SIZE);
			return;
		}// end function
		
		override protected function initUI() : void
		{
			super.initUI();
			this.track = new Button();
			this.track.setPosition(0, WIDTH);
			this.track.useHandCursor = false;
			this.track.autoRepeat = true;
			addChild(this.track);
			this.thumb = new LabelButton();
			this.thumb.label = "";
			this.thumb.setSize(WIDTH, 15);
			this.thumb.setPosition(0, 15);
			addChild(this.thumb);
			this.down = new Button();
			this.down.setSize(WIDTH, WIDTH);
			this.down.autoRepeat = true;
			addChild(this.down);
			this.up = new Button();
			this.up.setSize(WIDTH, WIDTH);
			this.up.setPosition(0, 0);
			this.up.autoRepeat = true;
			addChild(this.up);
			this.up.addEventListener(BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
			this.down.addEventListener(BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
			this.track.addEventListener(BUIEvent.BUTTON_REPEAT, this.handleScrollPress, false, 0, true);
			this.thumb.addEventListener(MouseEvent.MOUSE_DOWN, this.handleThumbPress, false, 0, true);
			this.enabled = false;
			return;
		}// end function
		
		override public function get height() : Number
		{
			return this._orientation == BUIConstants.HORIZONTAL ? (super.width) : (super.height);
		}// end function
		
		protected function handleThumbRelease(param1:MouseEvent) : void
		{
			this.isDragging = false;
			mouseChildren = true;
			this.thumb.mouseStateLocked = false;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.handleThumbDrag);
			stage.removeEventListener(MouseEvent.MOUSE_UP, this.handleThumbRelease);
			return;
		}// end function
		
		protected function handleScrollPress(param1:BUIEvent) : void
		{
			var _loc_2:* = NaN;
			var _loc_3:* = NaN;
			param1.stopImmediatePropagation();
			if (param1.currentTarget == this.up)
			{
				this.setScrollPosition(this._scrollPosition - this._lineScrollSize);
			}
			else if (param1.currentTarget == this.down)
			{
				this.setScrollPosition(this._scrollPosition + this._lineScrollSize);
			}
			else
			{
				_loc_2 = this.track.mouseY / this.track.height * (this._maxScrollPosition - this._minScrollPosition) + this._minScrollPosition;
				_loc_3 = this.pageScrollSize == 0 ? (this.pageSize) : (this.pageScrollSize);
				if (this._scrollPosition < _loc_2)
				{
					this.setScrollPosition(Math.min(_loc_2, this._scrollPosition + _loc_3));
				}
				else if (this._scrollPosition > _loc_2)
				{
					this.setScrollPosition(Math.max(_loc_2, this._scrollPosition - _loc_3));
				}// end else if
			}// end else if
			return;
		}// end function
		
		public function get lineScrollSize() : Number
		{
			return this._lineScrollSize;
		}// end function
		
		public function get pageSize() : Number
		{
			return this._pageSize;
		}// end function
		
		public function set maxScrollPosition(param1:Number) : void
		{
			this.setScrollProperties(this._pageSize, this._minScrollPosition, param1);
			return;
		}// end function
		
		public function set pageScrollSize(param1:Number) : void
		{
			if (param1 >= 0)
			{
				this._pageScrollSize = param1;
			}// end if
			return;
		}// end function
		
		public function set scrollPosition(param1:Number) : void
		{
			this.setScrollPosition(param1, true);
			return;
		}// end function
		
		override public function get width() : Number
		{
			return this._orientation == BUIConstants.HORIZONTAL ? (super.height) : (super.width);
		}// end function
		
		public function get orientation() : String
		{
			return this._orientation;
		}// end function
		
		override protected function draw() : void
		{
			var _loc_1:* = NaN;
			var _loc_2:* = undefined;
			if (isInvalid(Invalidation.SIZE))
			{
				_loc_1 = super.height;
				this.down.setPosition(0, Math.max(this.up.height, _loc_1 - this.down.height));
				this.track.setSize(WIDTH, Math.max(0, _loc_1 - (this.down.height + this.up.height)));
				this.updateThumb();
			}// end if
			if (isInvalid(Invalidation.STYLES))
			{
				_loc_2 = getSkinInstance(getStyleValue("skin"));
				this.track.setStyle("skin", _loc_2["track"]);
				this.thumb.setStyle("skin", _loc_2["thumb"]);
				this.thumb.setStyle("iconSkin", _loc_2["thumbIcon"]);
				this.down.setStyle("skin", _loc_2["down"]);
				this.up.setStyle("skin", _loc_2["up"]);
			}// end if
			this.down.drawNow();
			this.up.drawNow();
			this.track.drawNow();
			this.thumb.drawNow();
			validate();
			return;
		}// end function
		
		protected function handleThumbPress(param1:MouseEvent) : void
		{
			this.isDragging = true;
			this.thumbScrollOffset = mouseY - this.thumb.y;
			this.thumb.mouseStateLocked = true;
			mouseChildren = false;
			stage.addEventListener(MouseEvent.MOUSE_MOVE, this.handleThumbDrag, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_UP, this.handleThumbRelease, false, 0, true);
			return;
		}// end function
		
		public function get pageScrollSize() : Number
		{
			return this._pageScrollSize == 0 ? (this._pageSize) : (this._pageScrollSize);
		}// end function
		
		public function set pageSize(param1:Number) : void
		{
			if (param1 > 0)
			{
				this._pageSize = param1;
			}// end if
			return;
		}// end function
		
		public function setScrollProperties(param1:Number, param2:Number, param3:Number, param4:Number = 0) : void
		{
			this.pageSize = param1;
			this._minScrollPosition = param2;
			this._maxScrollPosition = param3;
			if (param4 >= 0)
			{
				this._pageScrollSize = param4;
			}// end if
			this.enabled = this._maxScrollPosition > this._minScrollPosition;
			this.setScrollPosition(this._scrollPosition, false);
			this.updateThumb();
			return;
		}// end function
		
		protected function handleThumbDrag(param1:MouseEvent) : void
		{
			var _loc_2:* = Math.max(0, Math.min(this.track.height - this.thumb.height, mouseY - this.track.y - this.thumbScrollOffset));
			this.setScrollPosition(_loc_2 / (this.track.height - this.thumb.height) * (this._maxScrollPosition - this._minScrollPosition) + this._minScrollPosition);
			param1.updateAfterEvent();
			return;
		}// end function
		
		override public function get classStyles() : Object
		{
			return mergeStyles(BUI.defaultStyles, defaultStyles);
		}// end function
		
	}
}
